# author: Jochen Rehmert, University of Basel
# journal: Journal of Politics
# article: Intra-party competition, geographic responsiveness 
#           and incumbent deselection in closed-list PR
# date: 12th August 2024
# content: Script to replicate Figure 14 in the Appendix
# needs data: "LANDKREISE_2009.shp" & "GN250_p.shp"


# directory
setwd("")

sink("log_rep_figure14.txt")
cat("start logging\n")
# packages
library(sf)
library(terra)

#Load shapefiles
kreis <- vect("LANDKREISE_2009.shp")

poi <- read_sf("GN250_p.shp")

# prepare files
poi$KREIS <- as.character(poi$KREIS)
poi$NAME2 <- as.character(poi$NAME2)
poi$VERWGEM <- as.character(poi$VERWGEM)
Encoding(poi$KREIS) <- "UTF-8"

##########################
# Figure 14 in the Appendix
##########################

dev.off()
cat("create figure 14\n")
par(mar = c(1, 1, 1, 1)) 
plot(kreis[kreis$LAND_NAME == "Niedersachsen","LAND_NAME"], main = "", col = "white", axes = FALSE)
plot(kreis[kreis$GEN == "Hildesheim","LAND_NAME"], add = TRUE, col = "grey")
plot(kreis[kreis$GEN == "Northeim",], add = TRUE, col = "grey")
plot(kreis[kreis$GEN == "Holzminden",], add = TRUE, col = "grey")
plot(kreis[kreis$GEN == "Hameln-Pyrmont",], add = TRUE, col = "grey")
plot(kreis[kreis$GEN == "Goslar",], add = TRUE, col = "grey")
plot(kreis[kreis$GEN == "Region Hannover",], add = TRUE, col = "grey")
plot(poi[poi$BUNDESLAND == "Niedersachsen" &  poi$VERWGEM %in% c("Hessisch Oldendorf"),], add = TRUE, col = "black", pch = 3)
plot(poi[poi$BUNDESLAND == "Niedersachsen" &  poi$VERWGEM %in% c("Emmerthal"),], add = TRUE, col = "black", pch = 3)
plot(poi[poi$BUNDESLAND == "Niedersachsen" &  poi$VERWGEM %in% c("Bad Gandersheim"),][c(1:10)], add = TRUE, col = "black", pch = 3)
plot(poi[poi$BUNDESLAND == "Niedersachsen" &  poi$VERWGEM %in% c("Bodenwerder-Polle"),], add = TRUE, col = "black", pch = 3)
plot(poi[poi$BUNDESLAND == "Niedersachsen" &  poi$VERWGEM %in% c("Hildesheim"),], add = TRUE, col = "black", pch = 3)
plot(poi[poi$BUNDESLAND == "Niedersachsen" &  poi$VERWGEM %in% c("Seesen"),], add = TRUE, col = "black", pch = 3)
plot(poi[poi$BUNDESLAND == "Niedersachsen" &  poi$VERWGEM %in% c("Springe"),], add = TRUE, col = "black", pch = 3)
plot(poi[poi$BUNDESLAND == "Niedersachsen" &  poi$VERWGEM %in% c("Wennigsen (Deister)"),], add = TRUE, col = "black", pch = 3)
plot(poi[poi$BUNDESLAND == "Niedersachsen" &  poi$VERWGEM %in% c("Nordstemmen"),], add = TRUE, col = "black", pch = 3)

cat("end logging")
sink()
